﻿using StaticDemo.Identity; 
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc; 
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using System;
using System.Collections;

namespace StaticDemo.Controllers
{
    public class AccountController : Controller
    {

        private IAuthenticationManager AuthenticationManager
        {
            get
            {
                return HttpContext.GetOwinContext().Authentication;
            }
        }
         
        public ApplicationSignInManager SignInManager
        {
            get
            {
                return HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
            } 
        }

        public ApplicationUserManager UserManager
        {
            get
            {
                return HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            } 
        } 

        // GET: Account
        public ActionResult Index()
        {
            return View();
        }
        public async Task<ActionResult> Login()
        {

            var claims = new[] {
                    new Claim(ClaimTypes.Email, "574427343@qq.com"),
                    new Claim(ClaimTypes.SerialNumber,"123456789"),
                    new Claim(ClaimTypes.Name,"张三"),
                };

            var claimsIdentity = new ClaimsIdentity(claims, "Cookie");
            ClaimsPrincipal user = new ClaimsPrincipal(claimsIdentity);

            //HttpContext.SignInAsync("Cookie", user, new AuthenticationProperties() { IsPersistent = true, ExpiresUtc = DateTimeOffset.Now.AddMinutes(60) }).Wait();

            //HttpContext.User = user;

            var appUser = new ApplicationUser { UserName = "张三01", Email = "123@qq.com",ExternalIdentity = claimsIdentity };

            var name = HttpContext.User.Identity.Name;


            HttpContext.Response.Cookies.Add(new HttpCookie("vs_vh") { Value ="hello",HttpOnly=true, Expires = DateTime.Now.AddMinutes(1) });

            if (!HttpContext.User.Identity.IsAuthenticated)
            {
                await SignInManager.SignInAsync(appUser, isPersistent: false, rememberBrowser: false);
            }

            //AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent=false }, claims);

            return RedirectToAction("Index", "Home");
        }
        public ActionResult LogOut()
        {
            AuthenticationManager.SignOut();
            HttpContext.Session.Abandon();
            removeAllCache();    

            return RedirectToAction("Index", "Home");
        }

        private void removeAllCache()
        {
            IDictionaryEnumerator DicCache = HttpRuntime.Cache.GetEnumerator();
            int count = HttpRuntime.Cache.Count;
            for (int i = 0; i < count; i++)
            {
                DicCache.MoveNext();
                HttpRuntime.Cache.Remove(DicCache.Entry.Key.ToString());
            }
        }
    }
}